package com.kdtech.analyse.outside;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * @author KK
 *
 */
public class BusinessweeklyTWAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://forum.businessweekly.com.tw/topic.aspx\\?fid=[0-9]*&tid=[0-9]*",
		"http://www.businessweekly.com.tw/[A-Za-z]*Article.aspx\\?ID=[0-9]*.*",
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		if (url.startsWith("http://forum.businessweekly.com.tw/")){
			title =doc.select("div.topic-title .article-title").text();
			content=HtmlCleaner.getContentHtml(url,doc.select(".article-content p"));
			date=DateUtils.matchDate(doc.select(".date").text());
			author=doc.select(".name a").text();
		}else{
			title =doc.select("title").text();
			if (title.contains("-")){
				title=StringUtils.substringBefore(title, "-");
			}
			content=HtmlCleaner.getContentHtml(url,doc.select("#articleBlock p"));
			date=DateUtils.matchDate(doc.select(".detail_info").text());
			author=JSoupUtils.matchAuthor(doc, "撰文者");
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	

	

	
}
